home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 12 / Cream of the Crop 12 (Part II) / Cream of the Crop 12 (Part II).iso / OS2 / VD08SMP.ZIP / usr / samples / textview / textview2.m < prev    next >
Encoding:
Text File  |  1996-02-13  |  2.7 KB  |  104 lines

  1. #include <pm/pm.h>
  2. #include <io.h>
  3. #include <sys/types.h>
  4. #include <sys/stat.h>
  5.  
  6. @interface Controller : Object
  7. {
  8. }
  9.  
  10. - windowDidResize: sender;
  11.  
  12. @end
  13.  
  14. @implementation Controller
  15.  
  16. - windowDidResize: sender
  17. {
  18.   [[sender findFromID: 1001] setSize: 0:0:[sender width]:[sender height]];
  19.   return self;
  20. }
  21.  
  22. @end
  23.  
  24. main(int argc,char *argv[])
  25. {
  26.   StdApp      *application;
  27.   StdWindow   *window;
  28.   Window      *mle;
  29.   Controller  *controller;
  30.   FILE        *inputFile;
  31.   struct stat  statbuffer;
  32.   char        *contents;
  33.   char        *title;
  34.  
  35.   /*
  36.    * check for command line arguments and check given file (struct stat)
  37.    */
  38.   if (argc != 2) /* check for command line arguments, must be exactly one */
  39.     exit (-1);
  40.  
  41.   if (stat (argv[1],&statbuffer) < 0) /* check file */
  42.     exit (-1);
  43.  
  44.   /*
  45.    * open file and read contents to buffer
  46.    */
  47.   inputFile = fopen (argv[1],"r"); /* open text file read-only */
  48.  
  49.   contents = (char *) malloc (statbuffer.st_size + 1); /* allocate buffer */
  50.   fread (contents,statbuffer.st_size,1,inputFile); /* read contents of file */
  51.  
  52.   /*
  53.    * create app instance and window, create MLE for text display
  54.    */
  55.   application = [[StdApp alloc] init]; /* initialize application object */
  56.   window = [[MainWindow alloc] initWithId: 1000
  57.                                 andFlags: (FCF_SIZEBORDER | FCF_TITLEBAR |
  58.                        FCF_SYSMENU | FCF_MINMAX |
  59.                        FCF_SHELLPOSITION | FCF_TASKLIST)];
  60.   /* create main window */
  61.   controller = [[Controller alloc] init];
  62.   
  63.   [window createObjects]; /* create child windows of main window */
  64.   [window setDelegate: controller];
  65.  
  66.   mle = [[MultiLineEntryField alloc] initWithId: 1001 
  67.                                        andFlags: (WS_VISIBLE | MLS_READONLY |
  68.                           MLS_HSCROLL | MLS_VSCROLL)
  69.                                              in: window];
  70.   [window insertChild: mle]; /* insert MLE into window */
  71.  
  72.   /*
  73.    * calculate title of window and set it
  74.    */
  75.   title = (char *) malloc (11 + /* this is the length of "Textview: " + NULL */
  76.                strlen (argv[1])); /* allocate buffer for title */
  77.   sprintf (title,"Textview: %s",argv[1]); /* fill title buffer */
  78.  
  79.   [window setTitle: title]; /* set window title */
  80.  
  81.   free (title); /* free title buffer */
  82.  
  83.   /*
  84.    * show window and display contents of file
  85.    */
  86.   [mle setText: contents]; /* display contents of file */
  87.   [window makeKeyAndOrderFront: nil]; /* show window */
  88.  
  89.   /*
  90.    * run application
  91.    */
  92.   [application run];
  93.  
  94.   /*
  95.    * free all resources
  96.    */
  97.   free (contents); /* free contents buffer */
  98.   fclose (inputFile); /* close file */
  99.  
  100.   [application free]; /* free application */
  101.   [window free]; /* free window */
  102.   [controller free]; /* free controller */
  103. }
  104.